import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MouseInputAdapter extends MouseAdapter {

        //这两组x和y为鼠标点下时在屏幕的位置和拖动时所在的位置
        int newX,newY,oldX,oldY;
        //这两个坐标为组件当前的坐标
        int startX,startY;

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("mousePressed");
            //此为得到事件源组件
            Component cp = (Component)e.getSource();
            //当鼠标点下的时候记录组件当前的坐标与鼠标当前在屏幕的位置
            startX = cp.getX();
            startY = cp.getY();
            oldX = e.getXOnScreen();
            oldY = e.getYOnScreen();
        }
        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("mouseDragged");
            Component cp = (Component)e.getSource();
            //拖动的时候记录新坐标
            newX = e.getXOnScreen();
            newY = e.getYOnScreen();
            //设置bounds,将点下时记录的组件开始坐标与鼠标拖动的距离相加
            cp.setBounds(startX+(newX - oldX), startY+(newY - oldY), cp.getWidth(), cp.getHeight());
        }


    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("mouseReleased");
    }
}
